from django.utils.deprecation import MiddlewareMixin

from my_apps.user.models import UserMessage


class CheckNewMessageMiddleware(MiddlewareMixin):
    """进入所有页面检查是否有新消息"""

    def process_request(self, request):
        if request.user.is_authenticated:
            all_new_message = UserMessage.objects.filter(to_user=request.user, has_read=False)
            if all_new_message:
                request.session['is_new_message'] = True
            else:
                request.session['is_new_message'] = False

    def process_response(self, request, response):
        return response
